package launcher;

import java.lang.reflect.Proxy;
import java.util.Locale;
import java.util.ResourceBundle;

import javafx.application.Application;
import javafx.stage.Stage;
import proxyHandler.OwnHandler;
import interfaces.AccountManager;
import command.UniversalCommandProcessor;
import stockPrice.HistoricalStockPriceProvider;
import stockPrice.RandomStockPriceProvider;
import ui.CommandReaderUI;
import viewer.Viewer;
import mainBoerse.AccountManagerImpl;

public class StockGameLauncher extends Application {

	private Stage stage;

	public static void main(String[] args) throws Throwable {
		Locale locale;
		if (args.length == 0) {
			args = new String[1];
			locale = Locale.GERMAN;
		} else if (args[0].equals("en")) {
			locale = Locale.ENGLISH;
		} else
			locale = Locale.GERMAN;
		// locale = Locale.ENGLISH;
		ResourceBundle bundle = ResourceBundle
				.getBundle("boerseBundle", locale);
		AccountManagerImpl impl = AccountManagerImpl.getInstance();

		// LoadRessources loadedRes = new LoadRessources(args[0]);
		// Properties properties = loadedRes.getProperties();
		/*
		 * System.out.println(player[0].toString());
		 */

		RandomStockPriceProvider provider = new RandomStockPriceProvider();
		HistoricalStockPriceProvider provider2 = new HistoricalStockPriceProvider();

		Viewer viewer = new Viewer(provider, impl, bundle);
		viewer.start();

		// setup proxy
		OwnHandler handler = new OwnHandler(impl);
		AccountManager impl2 = (AccountManager) Proxy.newProxyInstance(
				AccountManagerImpl.class.getClassLoader(),
				new Class[] { AccountManager.class }, handler);
		UniversalCommandProcessor uniproc = new UniversalCommandProcessor(
				impl2, AccountManager.class);

		(new Thread() {
			@Override
			public void run() {
				while (true) {
					try {
						uniproc.process();
					} catch (Throwable e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			};
		}).start();
		launch();
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		CommandReaderUI ui = new CommandReaderUI();
		ui.start(primaryStage);

	}

}
